package com.open.system.service.impl;

import com.open.system.mapper.NoticeMapper;
import com.open.system.model.Notice;
import com.open.common.model.Page;
import com.open.common.model.Sort;
import com.open.system.model.UserNotice;
import com.open.system.model.vo.NoticeVo;
import com.open.system.service.INoticeService;
import com.open.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author 大树03
 * @date 2025-07-18 17:38
 */
@Service(value = "noticeService")
@Slf4j
public class NoticeServiceImpl implements INoticeService {

	private NoticeMapper noticeMapper;

    @Autowired
    public void setNoticeMapper(NoticeMapper noticeMapper) {
        this.noticeMapper = noticeMapper;
    }

    @Override
    @Transactional()
    public Boolean save(Notice model)  {
        return noticeMapper.save(model);
    }

    @Override
    @Transactional()
    public Boolean update(Notice model)  {
        return noticeMapper.update(model);
    }

	@Override
	public Page<Notice> findPage(Page<Notice> page,Notice item)  {
        Map<String,Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex()-1)*page.getSize();
        map.put("orderBy",new Sort());
        map.put("item",item);
        map.put("startPoint",startPoint);
        map.put("endPoint",page.getSize());
        Integer count = noticeMapper.findPageCount(map);
        if (count > startPoint) {
            page.setItems(noticeMapper.findPage(map));
        }else {
            page.setItems(new ArrayList<Notice>());
        }
        page.setTotal(count);
        return page;
    }

	@Override
    public List<Notice> findByModel(Notice item)  {
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return noticeMapper.findByModel(map);
    }

	@Override
	public Notice findByPk(Long pk)  {
        return noticeMapper.findByPk(pk);
    }

    @Override
    public Integer countByModel(Notice item){
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return noticeMapper.findPageCount(map);
    }

    @Override
    @Transactional()
    public Boolean remove(List<Long> pks) {
        for (Long pk : pks) {
            noticeMapper.remove(pk);
        }
        return true;
    }

    @Override
    public Page<NoticeVo> scrollPage(Page<UserNotice> page, UserNotice model) {
        Page<NoticeVo> result = new Page<>();
        BeanUtils.copyProperties(page, result);
        Map<String,Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex()-1)*page.getSize();
        model.setSendTime(DateUtils.getCurrentTimeAsString());
        map.put("item",model);
        map.put("startPoint",startPoint);
        map.put("endPoint",page.getSize());
        result.setItems(noticeMapper.scrollPage(map));
        return result;
    }
}